import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import login from '../views/login.vue'
import home from '../views/home.vue'
import columnDetail from '../views/columnDetail.vue'
import startWriting from '../views/startWriting.vue'
import store from '../store/index'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    meta:{
      uhead:true,
      ufooter:true
    },
    component: home
  },
  {
    path:'/login',
    name:'login',
    meta:{
      uhead:false,
      ufooter:false
    },
    component:login
  },
  {
    path:'/columnDetail',
    name:'columnDetail',
    meta:{
      uhead:true,
      ufooter:true
    },
    component:columnDetail
  },
  {
    path:'/startWriting',
    name:'startWriting',
    meta:{
      uhead:true,
      ufooter:true,
      keepAlive:true
    },
    component:startWriting
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,form,next)=>{ // 路由守卫  用来控制登录权限 
  if(to.meta.keepAlive){
    if(store.state.user.isLogin){
      next()
    }else{
      next({name:'login'})
    }
  }else {
    next()
  }
})

export default router
